﻿Imports System.IO

Public Class frmDBOption
    Private si As Integer

    Sub view_database(ByVal host As String, ByVal port As Integer, ByVal username As String, ByVal password As String)
        Try
            Dim data As DataTable = mysql_show_databases(False, host, port, username, password)
            ListBoxControlDatabase.DataSource = data
            ListBoxControlDatabase.DisplayMember = "Database"
            ListBoxControlDatabase.ValueMember = "Database"
        Catch ex As Exception

        End Try
    End Sub

    Private Sub frmDBOption_FormClosed(sender As Object, e As FormClosedEventArgs) Handles Me.FormClosed
        Me.Dispose()
    End Sub

    Private Sub frmDBOption_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        If File.Exists(My.Application.Info.DirectoryPath.ToString & "\server.xml") = False Then client_write_configuration()
        client_read_configuration()
        If mysql_isconnected() Then
            Dim pt As New Point(0, 100)
            ListBoxControlDatabase.AutoScrollOffset = pt
            view_database(dbHost, dbPort, dbUser, dbPass)
            txtHost.Text = dbHost
            txtPort.Text = dbPort
            txtUsername.Text = dbUser
            txtPassword.Text = dbPass
            ListBoxControlDatabase.SelectedValue = dbName
            si = ListBoxControlDatabase.SelectedIndex

            SimpleButtonConnect.Text = "Change"

            txtHost.Enabled = False
            txtPort.Enabled = False
            txtUsername.Enabled = False
            txtPassword.Enabled = False
            SimpleButtonSave.Enabled = False
        Else
            txtHost.Text = dbHost
            txtPort.Text = dbPort
            txtUsername.Text = dbUser
            txtPassword.Text = dbPass

            txtHost.Enabled = True
            txtPort.Enabled = True
            txtUsername.Enabled = True
            txtPassword.Enabled = True
            SimpleButtonSave.Enabled = False
        End If
    End Sub

    Private Sub SimpleButtonConnect_Click(sender As Object, e As EventArgs) Handles SimpleButtonConnect.Click
        If SimpleButtonConnect.Text = "Change" Then
            SimpleButtonConnect.Text = "Connect"
            ListBoxControlDatabase.SelectedIndex = si
            SimpleButtonSave.Enabled = True
            txtHost.Enabled = True
            txtPort.Enabled = True
            txtUsername.Enabled = True
            txtPassword.Enabled = True

        Else
            If mysql_isconnected(False, txtHost.Text, Convert.ToInt32(txtPort.Text), txtUsername.Text, txtPassword.Text) Then
                view_database(txtHost.Text, Convert.ToInt32(txtPort.Text), txtUsername.Text, txtPassword.Text)
                SimpleButtonSave.Enabled = True
            Else
                ListBoxControlDatabase.DataSource = vbNull
            End If
        End If
    End Sub

    Private Sub SimpleButtonSave_Click(sender As Object, e As EventArgs) Handles SimpleButtonSave.Click
        If mysql_isconnected(False, txtHost.Text, Convert.ToInt32(txtPort.Text), txtUsername.Text, txtPassword.Text) And ListBoxControlDatabase.SelectedIndex > -1 And ListBoxControlDatabase.ItemCount > 0 Then
            If File.Exists(My.Application.Info.DirectoryPath.ToString & "\server.xml") = False Then client_write_configuration()
            client_update_configuration(New Object(0) {}, New Object(0) {}, reconfig_database(txtHost.Text, txtPort.Text, txtUsername.Text, txtPassword.Text, ListBoxControlDatabase.SelectedValue.ToString))
            client_read_configuration()
            txtHost.Enabled = False
            txtPort.Enabled = False
            txtUsername.Enabled = False
            txtPassword.Enabled = False

            SimpleButtonSave.Enabled = False
            SimpleButtonConnect.Text = "Change"
            si = ListBoxControlDatabase.SelectedIndex

        Else
            MsgBox("Cannot Save Configuration" & vbCrLf & "Please Check if Database is still connected and you have selected one database from Database List", MsgBoxStyle.Exclamation, "Warning")
        End If
    End Sub

    Private Sub ListBoxControlDatabase_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListBoxControlDatabase.SelectedIndexChanged
        If SimpleButtonConnect.Text = "Change" Then
            ListBoxControlDatabase.SelectedIndex = si
        End If
    End Sub
End Class